<?php

	// register widget
	add_action('widgets_init', create_function('', 'return register_widget("VnNews_Advertise");'));

	// Creating the widget
	class VnNews_Advertise extends WP_Widget {

		function __construct() {
			parent::__construct(
				'VnNews_Advertise',
				__('VnNews Advertise', 'vnnews_widget_advertise'),
				array( 'description' => __( 'VnNews Advertisement', 'vnnews_widget_advertise' ), )
			);
		}

		// Creating widget front-end
		// This is where the action happens
		public function widget( $args, $instance ) {
			global $wpdb;

			$title = apply_filters( 'widget_title', $instance['title'] );
			$category = apply_filters( 'widget_category', $instance['category'] );
			$numbers = apply_filters( 'widget_numbers', $instance['numbers'] );
			$enable_125_ads = apply_filters( 'widget_125_ads', $instance['125_ads'] );

			$img_class = ''; $div_in = ""; $end_div_in = "";
			if ($enable_125_ads == 'on') {
				$img_class = " widget-img-125";
				$div_in = "<div class='div-in-125 clearfix'>"; $end_div_in = "</div>";
			}

			$argadv = array('posts_per_page'=>$numbers,"post_type"=>"advertise",
							"post_status"=>"publish",
							"tax_query"=>array(array('taxonomy'=>"adv_categories",'field'=>'id','terms'=>$category)),
							"order" => 'ASC',
							"meta_key" => "adv_order",
							"orderby" => "meta_value");

            WP_Reset_Query();
            query_posts($argadv);

            if(have_posts()):

				echo $args['before_widget'];
				echo $div_in;

				while(have_posts()): the_post();
                    $idadv = get_the_ID();
                    $enable_google_ads = get_post_meta( $idadv, "adv_enable_google_ads", true );
                    $google_ads_code = get_post_meta( $idadv, "adv_google_ads", true );
                    $url = rwmb_meta('adv_url','type=image', $idadv);
                    $width = get_post_meta($idadv,'adv_width',true);
                    $height = get_post_meta($idadv,'adv_height',true);
                    $link = get_post_meta($idadv,'adv_link',true);
                    $target = get_post_meta($idadv,'adv_target',true);

                    if($enable_google_ads == '1')
                		echo $google_ads_code;
                	else{
	                    //if($type=='image'){
	                    	foreach ( $url as $image ):
	                	?>
	                    	<a href="<?php _e($link);?>" target="<?php _e($target);?>">
	                    		<img class="left<?php echo $img_class; ?>" src="<?php _e($image['full_url'])?>" alt="<?php _e(get_the_title());?>" <?php if($width != ""): _e("width='".$width."'"); endif;?> <?php if($height != ""): _e("height='".$height."'"); endif;?>/>
	                    	</a>
	                    <?php endforeach;
	            		/*} elseif($type=='flash'){?>

		                    <object width="<?php _e($width);?>" height="<?php _e($height);?>"
		                            classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
		                            codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">
		                        <param name="SRC" value="<?php _e($url);?>">
		                        <embed src="<?php _e($url);?>" width="<?php _e($width);?>" height="<?php _e($height);?>">
		                        </embed>
		                    </object>

						<?php } elseif($type=='video'){

		                    $arr=explode("=",$url);
		                    $arr2=explode("&",$arr[1]); ?>
		                    <object width="<?php _e($width);?>" height="<?php _e($height);?>">
		                    	<param name="movie" value="http://www.youtube.com/v/<?=$arr2[0]?>;hl=enamp;fs=1">
		                        </param><param name="allowFullScreen" value="true">
		                        </param><param name="allowscriptaccess" value="always"></param>
		                        <embed src="http://www.youtube.com/v/<?=$arr2[0]?>&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="<?php _e($width);?>" height="<?php _e($height);?>" autostart="false">
		                        </embed>
		                    </object>

						<?php } */
					}

                endwhile;

                echo $end_div_in;

				echo $args['after_widget'];

        	endif;

        	WP_Reset_Query();
		}

		// Widget Backend
		public function form( $options ) {

			global $wpdb;
            // options exist? if not set defaults
            if ( !is_array($options))
            {
                $options = array('title'=> __(''), 'category'=>'','numbers'=> '', '125_ads'=>'');
            }

            echo '<p>
				<label for="'. $this->get_field_id('title') .'">'. __('Title') .'
				<input id="'. $this->get_field_id('title') .'" name="'. $this->get_field_name('title') .'" type="text" class="widefat" value="'.strip_tags($options['title']).'" /></label></p>';
            echo '<p>
				<label for="'. $this->get_field_id('category') .'">'. __('Advertise category');

            echo '<select id="'. $this->get_field_id('category') .'" name="'. $this->get_field_name('category') .'" value="'. strip_tags($options['category']) .'">';
            $cat_id = -1;
            echo "<option value=\"-1\">". __('Choose advertise category') ."</option>";

            $results = get_terms('adv_categories', 'orderby=count&hide_empty=0');
            foreach ( $results as $term ):
            	$selected = "";
            	if( $options['category']==$term->term_id)
            		$selected = "selected='selected'";

            	echo "<option value='". $term->term_id ."'". $selected .">". $term->name ."</option>";

            endforeach;

            echo "</select>";

        	echo '<p>
				<label for="'. $this->get_field_id('numbers') .'">'. __('Number of advertises') .'
				<input id="'. $this->get_field_id('numbers') .'" name="'. $this->get_field_name('numbers') .'" type="text" class="widefat" value="'.strip_tags($options['numbers']).'" /></label></p>';

			$checked = '';
			if (strip_tags($options['125_ads']) == 'on') {
				$checked = 'checked';
			}
			echo '<p>
				<input id="'. $this->get_field_id('125_ads') .'" name="'. $this->get_field_name('125_ads') .'" type="checkbox" class="widefat" '. $checked .' />
				<label for="'. $this->get_field_id('125_ads') .'">'. __('Enable 125x125 ads') .'</label></p>';
		}

		// Updating widget replacing old instances with new
		public function update( $new_instance, $old_instance ) {
			$options = $old_instance;
            $options['title'] = strip_tags($new_instance['title']);
            $options['category'] = intval($new_instance['category']);
            $options['numbers'] = intval($new_instance['numbers']);
            $options['125_ads'] = strip_tags($new_instance['125_ads']);
            return $options;
		}
	}
	// Class VnNews_Advertise ends here
?>